<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/16
 * Time: 10:30
 */
namespace app\shop\controller;
use think\Controller;
use think\File;
use think\Request;
use think\Db;
class Upload extends Base
{
    public function test()
    {
        $task_sheng = -1;
        $task_sheng = $task_sheng==-1 ? 0 : $task_sheng;
        $task_endtime = '2019-04-24 11:57';
        $task_starttime = '2018-12-13 12:12';
        $cb = (strtotime($task_endtime) - strtotime($task_starttime))/3600;
        echo time();
        echo '<br/>';
        echo strtotime($task_endtime);
    }
    /**
     * 上传图片
     */
    public function uploadImg(){
        $file = request()->file('input_image');
        $yasuo = false;
        if (!$file->checkSize(100 * 1024)) { //大于100K就压缩一下
            $yasuo = true;
        }
        $dir = ROOT_PATH . 'public' . DS . 'uploads/shop/images';
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
        $url =  http_type ();
        $info = $file->move($dir);
        if($info){
            $newName = $info->getSaveName();
            if ($yasuo === true) { //大于100K就压缩一下
                $this->image_png_size_addshop($dir . '/' . $newName, $dir . '/' . $newName);
            }
            return json(['code'=>200,'msg'=>$url."/uploads/shop/images/{$newName}",'data'=>['src'=> "$url/uploads/shop/images/{$newName}"]]);
        }else{
            return json(['code'=>100,'msg'=>$file->getError(),'data'=>['src'=> ""]]);
        }
    }
    /**
     * 上传图片
     */
    public function uploadImg22(){
        $file = request()->file('input_image22');
        $yasuo = false;
        if (!$file->checkSize(100 * 1024)) { //大于100K就压缩一下
            $yasuo = true;
        }
        $dir = ROOT_PATH . 'public' . DS . 'uploads/shop/images';
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
        $url =  http_type ();
        $info = $file->move($dir);
        if($info){
            $newName = $info->getSaveName();
            if ($yasuo === true) { //大于100K就压缩一下
                $this->image_png_size_addshop($dir . '/' . $newName, $dir . '/' . $newName);
            }
            return json(['code'=>200,'msg'=>$url."/uploads/shop/images/{$newName}",'data'=>['src'=> "$url/uploads/shop/images/{$newName}"]]);
        }else{
            return json(['code'=>100,'msg'=>$file->getError(),'data'=>['src'=> ""]]);
        }
    }
    public function uploadImg1(){
        $file = request()->file('input_image1');
        $yasuo = false;
        if (!$file->checkSize(100 * 1024)) { //大于100K就压缩一下
            $yasuo = true;
        }
        $dir = ROOT_PATH . 'public' . DS . 'uploads/shop/images';
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
        $url =  http_type ();
        $info = $file->move($dir);
        if($info){
            $newName = $info->getSaveName();
            if ($yasuo === true) { //大于100K就压缩一下
                $this->image_png_size_addshop($dir . '/' . $newName, $dir . '/' . $newName);
            }
            return json(['code'=>200,'msg'=>$url."/uploads/shop/images/{$newName}",'data'=>['src'=> "$url/uploads/shop/images/{$newName}"]]);
        }else{
            return json(['code'=>100,'msg'=>$file->getError(),'data'=>['src'=> ""]]);
        }
    }

    /**
     * 上传视频
     */
    public function uploadVideo(){
        //引入getid3媒体类
        vendor('getid3.getid3');
        $file = request()->file('input_video');
        if(!$file){
            return json(['code'=>100,'msg'=>'请选择视频']);
        }
        $info  = $file->getInfo();
        $size  = $file->getSize();
        if(($size/1024/1024) > 100){
            return json(['code'=>100,'msg'=>'视频大小不得超过100M']);
        }
        $mp3 = new \getID3();
        $info =  $mp3->analyze($info['tmp_name']);
        $long = (int)floor($info['playtime_seconds']);//获取上传视频时长
        if($long < 15){
            return json(['code'=>100,'msg'=>'请上传15s以上的视频']);
        }
        $dir = ROOT_PATH . 'public' . DS . 'uploads/shop/video';
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
        $info = $file->move($dir);
        $url =  http_type ();
        if($info){
            $newName = $info->getSaveName();
            return json(['code'=>200,'msg'=>"$url/uploads/shop/video/{$newName}",'data'=>['src'=> "$url/uploads/shop/video/{$newName}"]]);
        }else{
            return json(['code'=>100,'msg'=>$file->getError(),'data'=>['src'=> ""]]);
        }
    }

    /**
     * uploadLocality图片上传至本地&压缩
     */
    public function uploadLocality(){
        $file = request()->file('file');
        $dir = ROOT_PATH . 'public' . DS . 'uploads/images';
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
//        $url =  http_type ();
        $info = $file->move($dir);
        if($info){
            $newName = $info->getSaveName();
            //压缩图片
//            image_png_size_add(ROOT_PATH . 'public' . DS . 'uploads/images/'.$newName,ROOT_PATH . 'public' . DS . 'uploads/images/'.$newName);
            echo "/uploads/images/{$newName}";
        }else{
            echo $file->getError();
        }
    }

    /**
     * deleteLocality 删除本地图片
     * @return \think\response\Json
     */
    public function deleteLocality(){
        $add  = input('add');
        $add = substr ($add,1);
        if(unlink($add)){
            return json(['code'=>200,'msg'=>'删除成功！']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败！']);
        }
    }
    /*
        * wangEditor图片上传
        */
    public function wangUpload(){
        set_time_limit (0);
        $files = request()->file();
        $dir = ROOT_PATH . 'public' . DS . 'uploads/images';
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
        $url =  http_type ();
        foreach($files as $file){
            $yasuo = false;
            if (!$file->checkSize(100 * 1024)) { //大于100K就压缩一下
                $yasuo = true;
            }
            $info = $file->move($dir);
            if($info){
                $newName = $info->getSaveName();
                if ($yasuo === true) { //大于100K就压缩一下
                    $this->image_png_size_addshop($dir . '/' . $newName, $dir . '/' . $newName);
                }
                $data[] = $url."/uploads/images/{$newName}";
            }
        }
        return json(['errno'=>0,'data'=>$data]);
    }
}